﻿<%@ Control Language="C#" AutoEventWireup="true"
    Inherits="PayrollHR.Administration.Modules.DecimalTextBox" Codebehind="DecimalTextBox.ascx.cs" %> 
<asp:TextBox ID="txtValue" runat="server" Width="80px" onkeyup = "ValidateAndFormatNumber(this, event.keyCode)" onkeydown = "return NumberValidate(this, event.keyCode)" ></asp:TextBox>
<script language="javascript" type="text/javascript">    
    function NumberValidate(txt, keyCode) {
        if (keyCode == 16)
            isShift = true;
        //Validate that its Numeric
        if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 ||
         keyCode <= 37 || keyCode <= 39 ||
         (keyCode >= 96 && keyCode <= 105)) && isShift == false) {           
            return true;
        }
        else {
            return false;
        }
    }
    function ValidateAndFormatNumber(NumberTextBox) {
        if (NumberTextBox.value == "") return;

        NumberTextBox.value = NumberTextBox.value.replace(/\s+/g, '');
        UnFormatNumber(NumberTextBox);

        var IsFound = /^-?\d+\.{0,1}\d*$/.test(NumberTextBox.value);
        if (!IsFound) {
            NumberTextBox = "";
            NumberTextBox.focus();
            NumberTextBox.select();
            return;
        }

        if (isNaN(parseFloat(NumberTextBox.value))) {
            alert("Number exceeding float range");
            NumberTextBox.focus();
            NumberTextBox.select();
        }

        NumberTextBox.value = FormatNumber(NumberTextBox.value);
    }

    function FormatNumber(fnum) {
        var orgfnum = fnum;
        var flagneg = false;

        if (fnum.charAt(0) == "-") {
            flagneg = true;
            fnum = fnum.substr(1, fnum.length - 1);
        }

        psplit = fnum.split(".");

        var cnum = psplit[0],
	            parr = [],
	            j = cnum.length,
	            m = Math.floor(j / 3),
	            n = cnum.length % 3 || 3;

        // break the number into chunks of 3 digits; first chunk may be less than 3
        for (var i = 0; i < j; i += n) {
            if (i != 0) { n = 3; }
            parr[parr.length] = cnum.substr(i, n);
            m -= 1;
        }

        // put chunks back together, separated by comma
        fnum = parr.join(",");

        // add the precision back in
        //if (psplit[1]) {fnum += "." + psplit[1];}
        if (orgfnum.indexOf(".") != -1) {
            fnum += "." + psplit[1];
        }

        if (flagneg == true) {
            fnum = "-" + fnum;
        }

        return fnum;
    }

    function UnFormatNumber(obj) {
        if (obj.value == "") return;

        obj.value = obj.value.replace(/,/gi, "");
    }

    function getCaretPosition(objTextBox) {

        var objTextBox = window.event.srcElement;

        var i = objTextBox.value.length;

        if (objTextBox.createTextRange) {
            objCaret = document.selection.createRange().duplicate();
            while (objCaret.parentElement() == objTextBox &&
                  objCaret.move("character", 1) == 1) --i;
        }
        return i;
    }

    function setSelectionRange(input, selectionStart, selectionEnd) {
        if (input.setSelectionRange) {
            input.focus();
            input.setSelectionRange(selectionStart, selectionEnd);
        }
        else if (input.createTextRange) {
            var range = input.createTextRange();
            range.collapse(true);
            range.moveEnd('character', selectionEnd);
            range.moveStart('character', selectionStart);
            range.select();
        }
    }
    </script>